<?php
namespace submodule\modules\community\components;

use Yii;
use yii\db\Schema;
use yii\db\Migration;

class Uninstaller extends Migration
{
	public $controller;
	
	public function __construct($controller){
		$this->controller = $controller;
		
	}
    //安装
    public function install($models, $field_tablename)
    {
		$moduleid = $models[0]->modelid;
		$modelid = isset($models[1]) ? $models[1]->modelid : false;
		$modelids = [$moduleid, $modelid];
		$tablename = $models[0]->tablename;
		//建表
        Yii::$app->db->createCommand()->createTable('{{%'.$tablename.'}}', [
			'cid'=> "pk",
			'parentid'=> "smallint(5) unsigned NOT NULL default '0'",
			'name'=> "char(80) NOT NULL default '' COMMENT '版块名称'",
			'listorder'=> "tinyint(3) unsigned NOT NULL default '0' COMMENT '版块排序'",
			'status'=> "tinyint(1) unsigned NOT NULL default '1' COMMENT '版块状态'",
			'icon'=> "char(150) NOT NULL default '' COMMENT '版块图标'",
			'addtime'=> "int(10) unsigned NOT NULL default '0'",
			'moderator'=> "varchar(150) default '' COMMENT '版主'",
			'posts'=> "int(10) unsigned NOT NULL default '0' COMMENT '帖子数量'",
			'follows'=> "int(10) unsigned NOT NULL default '0' COMMENT '关注数'",
			'lastpost'=> "int(10) unsigned NOT NULL default '0' COMMENT '最后发帖'",
			'intro'=> "varchar(255) default '' COMMENT '版块描述'",
			'recommend'=> "tinyint(1) default '0' COMMENT '是否推荐'",
        ],'ENGINE InnoDB DEFAULT CHARSET utf8')->execute();
		
		//新建帖子表
       Yii::$app->db->createCommand()->createTable('{{%'.$tablename.'_post}}', [
            'postid'=>  "pk",
			'forumid'=> "smallint(5) unsigned NOT NULL default '0' COMMENT '版块ID'",
			'subject'=> "char(80) NOT NULL default '' COMMENT '帖子主题'",
			'listorder'=> "tinyint(3) unsigned NOT NULL default '0' COMMENT '版块排序'",
			'status'=> "tinyint(1) unsigned NOT NULL default '1' COMMENT '版块状态'",
			'anonymous'=> "tinyint(1) NOT NULL default '0' COMMENT '是否匿名'",
			'addtime'=> "int(10) unsigned NOT NULL default '0' COMMENT '发布时间'",
			'replys'=> "int(10) default '0' COMMENT '回复数量'",
			'likes'=> "int(10) default '0' COMMENT '点赞数量'",
			'views'=> "int(10) unsigned NOT NULL default '0' COMMENT '浏览数'",
			'follows'=> "int(10) unsigned NOT NULL default '0' COMMENT '关注数'",
			'images'=> "varchar(255) default '' COMMENT '图片'",
			'content'=> "text default NULL COMMENT '帖子内容'",
			'userid'=> "int(10) unsigned NOT NULL default '0' COMMENT '用户id'",
        ],'ENGINE InnoDB DEFAULT CHARSET utf8')->execute();
		
		//回复表
        Yii::$app->db->createCommand()->createTable('{{%'.$tablename.'_reply}}', [
			'replyid'=> "pk",
			'forumid'=> "smallint(5) unsigned NOT NULL default '0' COMMENT '版块ID'",
			'postid'=> "smallint(5) unsigned NOT NULL default '0' COMMENT '帖子ID'",
			'status'=> "tinyint(1) unsigned NOT NULL default '1' COMMENT '版块状态'",
			'anonymous'=> "tinyint(1) NOT NULL default '0' COMMENT '是否匿名'",
			'addtime'=> "int(10) unsigned NOT NULL default '0' COMMENT '发布时间'",
			'likes'=> "int(10) default '0' COMMENT '点赞数量'",
			'content'=> "text default NULL COMMENT '帖子内容'",
			'userid'=> "int(10) unsigned NOT NULL default '0' COMMENT '用户id'",
        ],'ENGINE InnoDB DEFAULT CHARSET utf8')->execute();
        
		
		
		//版块表字段
		$sql[] = "INSERT INTO $field_tablename  (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, 'ID', 'cid', 'primarykey', 'int', '10', 0, 0, 1, 1, '', '', NULL, 1)";

		$sql[] = "INSERT INTO $field_tablename  (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, '父版块', 'parentid', 'category', 'int', '10', 0, 0, 1, 1, '', '', NULL, 1)";
		
		$sql[] = "INSERT INTO $field_tablename  (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, '版块名称', 'name', 'text', 'varchar', '80', 0, 0, 1, 1, '', '', NULL, 1)";
		
		$sql[] = "INSERT INTO $field_tablename  (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, '排序', 'listorder', 'text', 'varchar', '80', 0, 0, 1, 1, '', '', NULL, 0)";
		
		$sql[] = "INSERT INTO $field_tablename  (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, '状态', 'status', 'switch', 'tinyint', '1', 0, 0, 1, 1, '', '', NULL, 1)";
		
		$sql[] = "INSERT INTO $field_tablename  (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, '版块图标', 'icon', 'image', 'varchar', '150', 0, 0, 1, 1, '', '', NULL, 1)";
		
		
		$sql[] = "INSERT INTO $field_tablename  (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, '添加时间', 'addtime', 'datetime', 'int', '10', 0, 0, 1, 1, '', '', NULL, 1)";
		
		$sql[] = "INSERT INTO $field_tablename  (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, '版主', 'moderator', 'text', 'varchar', '150', 0, 0, 1, 1, '', '', NULL, 1)";
		
		
		$sql[] = "INSERT INTO $field_tablename  (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, '帖子数量', 'posts', 'text', 'int', '10', 0, 0, 1, 1, '', '', NULL, 0)";
		
		
		$sql[] = "INSERT INTO $field_tablename  (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, '关注数量', 'follows', 'text', 'int', '10', 0, 0, 1, 1, '', '', NULL, 0)";
		
		$sql[] = "INSERT INTO $field_tablename  (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, '最后发帖时间', 'lastpost', 'datetime', 'int', '10', 0, 0, 1, 1, '', '', NULL, 0)";
		
		
		$sql[] = "INSERT INTO $field_tablename  (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, '版块介绍', 'intro', 'textarea', 'varchar', '255', 0, 0, 1, 1, '', '', NULL, 1)";
		
		
		$sql[] = "INSERT INTO $field_tablename (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, '关注数量', 'recommend', 'switch', 'tinyint', '1', 0, 0, 1, 1, '', '', NULL, 1)";
        foreach($sql as $v){
			Yii::$app->db->createCommand($v)->execute();
		}
		
		
		
		//写入models模型表	
		$model = new \submodule\models\Models;
		$attributes = [
			'isdefault'=> 0,
			'name'=> '发帖',
			'tablename'=> $tablename.'_post',
			'type'=> 'community',
		];
		$model->isNewRecord = true;
		$model->setAttributes($attributes);
		 
		if($model->save()){
			$modelid = $model->modelid;
			$modelids[] = $modelid;
			
		}else{
			
		}
		
		
		//============================
		//帖子表字段
		$sql = [];
		$sql[] = "INSERT INTO $field_tablename (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, 'ID', 'postid', 'primarykey', 'int', '10', 0, 0, 1, 1, '', '', NULL, 1)";
		
		$sql[] = "INSERT INTO $field_tablename (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, '版块ID', 'forumid', 'category', 'int', '10', 0, 0, 1, 1, '', '', NULL, 1)";
		
		$sql[] = "INSERT INTO $field_tablename (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, '帖子主题', 'subject', 'text', 'varchar', '80', 0, 0, 1, 1, '', '', NULL, 1)";
		
		$sql[] = "INSERT INTO $field_tablename (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, '排序', 'listorder', 'text', 'varchar', '80', 0, 0, 1, 1, '', '', NULL, 0)";
		
		$sql[] = "INSERT INTO $field_tablename (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, '状态', 'status', 'switch', 'tinyint', '1', 0, 0, 1, 1, '', '', NULL, 1)";
		
		$sql[] = "INSERT INTO $field_tablename (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, '是否匿名', 'anonymous', 'switch', 'tinyint', '1', 0, 0, 1, 1, '', '', NULL, 1)";
		
		
		$sql[] = "INSERT INTO $field_tablename (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, '添加时间', 'addtime', 'datetime', 'int', '10', 0, 0, 1, 1, '', '', NULL, 1)";
		
		$sql[] = "INSERT INTO $field_tablename (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, '回复数量', 'replys', 'text', 'int', '10', 0, 0, 1, 1, '', '', NULL, 1)";
		
		$sql[] = "INSERT INTO $field_tablename (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, '点赞数量', 'likes', 'text', 'int', '10', 0, 0, 1, 1, '', '', NULL, 1)";
		
		$sql[] = "INSERT INTO $field_tablename (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, '浏览数量', 'views', 'text', 'int', '10', 0, 0, 1, 1, '', '', NULL, 0)";
		
		
		$sql[] = "INSERT INTO $field_tablename (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, '关注数量', 'follows', 'text', 'int', '10', 0, 0, 1, 1, '', '', NULL, 0)";
		
		$sql[] = "INSERT INTO $field_tablename (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, '图片', 'images', 'image', 'varchar', '150', 0, 0, 1, 1, '', '', NULL, 1)";
		
		
		$sql[] = "INSERT INTO $field_tablename (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, '帖子内容', 'content', 'textarea', 'text', '', 0, 0, 1, 1, '', '', NULL, 1)";
		
		
		$sql[] = "INSERT INTO $field_tablename (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, '用户id', 'userid', 'text', 'int', '10', 0, 0, 1, 1, '', '', NULL, 1)";
		foreach($sql as $v){
			Yii::$app->db->createCommand($v)->execute();
		}
		//写入models模型表	
		$model = new \submodule\models\Models;
		$attributes = [
			'isdefault'=> 0,
			'name'=> '回帖',
			'tablename'=> $tablename.'_reply',
			'type'=> 'community',
		];
		$model->isNewRecord = true;
		$model->setAttributes($attributes);
		 
		if($model->save()){
			$modelid = $model->modelid;
			$modelids[] = $modelid;
		}else{
			
		}
		$sql = [];
		$sql[] = "INSERT INTO $field_tablename (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, 'ID', 'replyid', 'primarykey', 'int', '10', 0, 0, 1, 1, '', '', NULL, 1)";
		
		$sql[] = "INSERT INTO $field_tablename (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, '版块ID', 'forumid', 'category', 'int', '10', 0, 0, 1, 1, '', '', NULL, 1)";
		
		$sql[] = "INSERT INTO $field_tablename (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, '帖子ID', 'postid', 'text', 'int', '10', 0, 0, 1, 1, '', '', NULL, 1)";
		
		$sql[] = "INSERT INTO $field_tablename (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, '状态', 'status', 'switch', 'tinyint', '1', 0, 0, 1, 1, '', '', NULL, 1)";
		
		$sql[] = "INSERT INTO $field_tablename (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, '是否匿名', 'anonymous', 'switch', 'tinyint', '1', 0, 0, 1, 1, '', '', NULL, 1)";
		
		$sql[] = "INSERT INTO $field_tablename (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, '添加时间', 'addtime', 'datetime', 'int', '10', 0, 0, 1, 1, '', '', NULL, 1)";
		
		$sql[] = "INSERT INTO $field_tablename (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, '点赞数量', 'likes', 'text', 'int', '10', 0, 0, 1, 1, '', '', NULL, 1)";
		
		$sql[] = "INSERT INTO $field_tablename (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, '回复内容', 'content', 'textarea', 'text', '', 0, 0, 1, 1, '', '', NULL, 1)";
		
		$sql[] = "INSERT INTO $field_tablename (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, '用户id', 'userid', 'text', 'int', '10', 0, 0, 1, 1, '', '', NULL, 1)";
		foreach($sql as $v){
			Yii::$app->db->createCommand($v)->execute();
		}
		return $modelids;
		
    }
	
	//新建模型
	public function create($option){
		
		return [$option['modelid']];
	}
    
	
	//卸载
    public function uninstall()
    {
        $this->dropTable('{{store_product}}');
    }
}
